package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.MassagistWithdrawRecordCondition;
import com.spa.infrastructure.repository.mapper.MassagistWithdrawRecordMapper;
import com.spa.infrastructure.repository.po.MassagistWithdrawRecordPO;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MassagistWithdrawRecordRepositoryService extends ServiceImpl<MassagistWithdrawRecordMapper, MassagistWithdrawRecordPO> {

    @Autowired
    private MassagistWithdrawRecordMapper massagistWithdrawRecordMapper;

    public void add(MassagistWithdrawRecordPO po) {
        massagistWithdrawRecordMapper.insert(po);
    }

    public MassagistWithdrawRecordPO getMassagistWithdrawRecordPOById(Long id) {
        return getById(id);
    }

    public Page<MassagistWithdrawRecordPO> page(MassagistWithdrawRecordCondition condition){
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    public List<MassagistWithdrawRecordPO> list(MassagistWithdrawRecordCondition condition){
        return list(wrapper(condition));
    }

    public void updateMassagistWithdrawRecord(MassagistWithdrawRecordPO po) {
        update(null, updateWrapper(po));
    }

    private LambdaQueryWrapper<MassagistWithdrawRecordPO> wrapper(MassagistWithdrawRecordCondition condition) {
        LambdaQueryWrapper<MassagistWithdrawRecordPO> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(StringUtils.isNotEmpty(condition.getBeginTime()), MassagistWithdrawRecordPO::getApplyTime, condition.getBeginTime());
        wrapper.le(StringUtils.isNotEmpty(condition.getEndTime()), MassagistWithdrawRecordPO::getApplyTime, condition.getEndTime());
        wrapper.like(StringUtils.isNotEmpty(condition.getMassagistName()), MassagistWithdrawRecordPO::getMassagistName, condition.getMassagistName());
        wrapper.like(StringUtils.isNotEmpty(condition.getRealName()), MassagistWithdrawRecordPO::getRealName, condition.getRealName());
        wrapper.eq(ObjectUtil.isNotNull(condition.getStatus()), MassagistWithdrawRecordPO::getStatus, condition.getStatus());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getStatusList()), MassagistWithdrawRecordPO::getStatus, condition.getStatusList());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getIds()), MassagistWithdrawRecordPO::getId, condition.getIds());
        wrapper.eq(ObjectUtil.isNotNull(condition.getShopId()), MassagistWithdrawRecordPO::getShopId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getMassagistId()), MassagistWithdrawRecordPO::getMassagistId, condition.getMassagistId());
        wrapper.orderByDesc(MassagistWithdrawRecordPO::getApplyTime);
        return wrapper;
    }

    private LambdaUpdateWrapper<MassagistWithdrawRecordPO> updateWrapper(MassagistWithdrawRecordPO po) {
        LambdaUpdateWrapper<MassagistWithdrawRecordPO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(MassagistWithdrawRecordPO::getId, po.getId());
        updateWrapper.set(ObjectUtil.isNotNull(po.getStatus()), MassagistWithdrawRecordPO::getStatus, po.getStatus());
        updateWrapper.set(ObjectUtil.isNotNull(po.getAuditTime()), MassagistWithdrawRecordPO::getAuditTime, po.getAuditTime());
        updateWrapper.set(ObjectUtil.isNotNull(po.getPayTime()), MassagistWithdrawRecordPO::getPayTime, po.getPayTime());
        updateWrapper.set(ObjectUtil.isNotNull(po.getAuditName()), MassagistWithdrawRecordPO::getAuditName, po.getAuditName());
        updateWrapper.set(ObjectUtil.isNotNull(po.getAuditId()), MassagistWithdrawRecordPO::getAuditId, po.getAuditId());
        updateWrapper.set(StringUtils.isNotEmpty(po.getCredentials()), MassagistWithdrawRecordPO::getCredentials, po.getCredentials());
        return updateWrapper;
    }

}
